package com.hoko.blur.filter;

import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import com.hoko.blur.HokoBlur;
import com.hoko.blur.anno.Direction;
import com.hoko.blur.anno.Mode;

import static com.hoko.blur.util.BitmapUtil.replaceBitmap;


/**
 * 起源模糊滤波器
 *
 * @since 2021/08/03
 */
public final class OriginBlurFilter {

    /**
     * 做模糊
     *
     * @param mode 模式
     * @param bitmap 位图
     * @param radius 半径
     * @param cores 核
     * @param index 指数
     * @param direction 方向
     */
    public static void doBlur(@Mode int mode, PixelMap bitmap, int radius, int cores, int index, @Direction int direction) {
        System.out.println("zhangliang==>doBlur"+mode);
        int w = bitmap.getImageInfo().size.width;
        int h = bitmap.getImageInfo().size.height;

        int x = 0;
        int y = 0;
        int deltaX = 0;
        int deltaY = 0;

        if (direction == HokoBlur.HORIZONTAL) {
            deltaY = h / cores;
            y = index * deltaY;

            if (index == cores - 1) {
                deltaY = h - (cores - 1) * deltaY;
            }

            deltaX = w;
        } else if (direction == HokoBlur.VERTICAL) {
            deltaX = w / cores;
            x = index * deltaX;

            if (index == cores - 1) {
                deltaX = w - (cores - 1) * deltaX;
            }

            deltaY = h;
        }


        final int[] pixels = new int[deltaX * deltaY];
//        bitmap.getPixels(pixels, 0, deltaX, x, y, deltaX, deltaY);
        bitmap.readPixels(pixels,0,deltaX,new Rect(x,y,deltaX, deltaY));
        switch (mode) {
            case HokoBlur.MODE_BOX:
                BoxBlurFilter.doBlur(pixels, deltaX, deltaY, radius, direction);
                break;

            case HokoBlur.MODE_GAUSSIAN:
                GaussianBlurFilter.doBlur(pixels, deltaX, deltaY, radius, direction);
                break;

            case HokoBlur.MODE_STACK:
                StackBlurFilter.doBlur(pixels, deltaX, deltaY, radius, direction);
                break;
        }
        if (bitmap.isEditable()) {
            System.out.println("zhangliang==>isEditable="+bitmap.isEditable());
//            bitmap.setPixels(pixels, 0, deltaX, x, y, deltaX, deltaY);
            bitmap.writePixels(pixels,0,deltaX,new Rect(x,y,deltaX, deltaY));
        } else {
            replaceBitmap(bitmap, pixels, x, y, deltaX, deltaY);
        }

    }

    /**
     * 做完整的模糊
     *
     * @param mode 模式
     * @param bitmap 位图
     * @param radius 半径
     */
    public static void doFullBlur(@Mode int mode, PixelMap bitmap, int radius) {
        int w = bitmap.getImageInfo().size.width;
        int h = bitmap.getImageInfo().size.height;
        System.out.println("zhangliang==>1"+w);
        final int[] pixels = new int[w * h];
//        bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
        bitmap.readPixels(pixels,0,w,new Rect(0,0,0, 0));
        switch (mode) {
            case HokoBlur.MODE_BOX:
                System.out.println("zhangliang==>2");
                BoxBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
                break;

            case HokoBlur.MODE_GAUSSIAN:
                System.out.println("zhangliang==>3");
                GaussianBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
                break;

            case HokoBlur.MODE_STACK:
                System.out.println("zhangliang==>4");
                StackBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
                break;
        }
        if (bitmap.isEditable()) {
            System.out.println("zhangliang==>5");
//            bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
//            bitmap.writePixels(pixels,0,w,new Rect(0, 0, w, h));
            PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
            initializationOptions.size = new Size(w, h);
            initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
            initializationOptions.editable = true;
            bitmap = PixelMap.create(pixels, initializationOptions);
        } else {
            replaceBitmap(bitmap, pixels, 0, 0, w, h);
        }
    }
}
